(function (jQuery) {
	$(function () {
        // 加载验证码
        $.idcode.setCode();

        // 字段验证
        $.validator.addMethod("checkName", function(val, ele){
            return /^[a-z]\w{5,19}/i.test(val)
        })
        $.validator.addMethod("checkTel", function(val, ele){
            return /^[1]\d{10}$/.test(val);
        })
        $.validator.addMethod("checkRelname", function(val, ele){
            return /[\u4e00-\u9fa5]+/.test(val);
        })
        $.validator.addMethod("checkEmail", function(val, ele){
            return /^\w+@\w+\.\w+$/.test(val);
        })
        // 表单验证
        $("form#register").validate({
            rules: {
                uname : {
                    required : true,
                    checkName : true,
                    remote : {
                        url : "../../server/src/chechname.php",
                        type : "get",
                        dataType : "json",
                        data : {
                            uname : function(){
                                return $("[name=uname]").val();
                            }
                        }
                    }
                },
                upwd: {
                    required: true
                },
                upwd2: {
                    required : true,
                    equalTo : "#upwd",
                },
                utel: {
                    required: true,
                    checkTel: true
                },
                urelname: {
                    required: true,
                    checkRelname: true
                },
                uemail: {
                    checkEmail : true
                }
            },
            messages: {
                uname: {
                    required : "用户名必填",
                    checkName : "字母开头6-20位",
                    remote : "用户名已存在"
                },
                upwd: {
                    required: "请输入密码"
                },
                upwd2: {
                    required: "请再次输入密码",
                    equalTo : "2次密码不一致"
                },
                utel: {
                    required: "请输入电话",
                    checkTel: "11位电话号码"
                },
                urelname: {
                    required: "请输入真实姓名",
                    checkRelname: "中文"
                },
                uemail: {
                    checkEmail : "格式错误"
                }
            },
            submitHandler : function(){
                if (!$("input#agreen").prop("checked")) {
                    alert('未同意协议');
                    return;
                }
                if(!$.idcode.validateCode()){
                    alert("验证码错误");
                    return;
                }

                var formdata = new FormData(); // 是一个对象  {key,value}
                formdata.append("uimg", $("input[type=file]")[0].files[0]);
                formdata.append("uname", $("[name=uname]").val());
                formdata.append("urelname", $("[name=urelname]").val());
                formdata.append("upwd", $("[name=upwd]").val());
                formdata.append("ugender", $("[name=ugender]:checked").val());
                formdata.append("uemail", $("[name=uemail]").val());
                formdata.append("utel", $("[name=utel]").val());
                $.ajax({
                    url : "../../server/src/register.php",
                    type : "post",
                    data : formdata,
                    //文件上传
                    processData: false,
                    contentType: false,
                    success: function (res) {
                        alert(res.msg)
                        if(res.status == 1){
                            if(confirm("是否登录？")){
                                $.ajax({
                                    url: '../../server/src/login.php',
                                    type: 'POST',
                                    dataType: 'json',
                                    data: {
                                        uname: $("[name=uname]").val(),
                                        upwd: $("[name=upwd]").val()
                                    }
                                }).then(function (res) {
                                    if (res.status==1) { 
                                        var resdata = res.data;
                                        $.cookie("usercount", JSON.stringify(resdata[0]),{expires:10});                                   
                                        window.location = 'home.html';
                                    }
                                })
                            }
                        }
                    }
                })

                return false;
            },
            errorPlacement : function(err, ele){
                // ele.parent("div").append('<i class="falseImg"></i>');
                ele.parent("div").append(err);
            },
            success : function(err, ele){
                // $(ele).parent("div").find('i.falseImg').remove();
                // $(ele).parent("div").append('<i class="trueImg"></i>');
            }
        })

        // 图片显示
        $("input[type=file]").on("change", function(){
            if(/(jpeg|gif|png|jpg)/.test(this.files[0].type)){
                var read = new FileReader();
                read.readAsDataURL(this.files[0]);
                read.onload = function(res){
                    $("img.uavatar").attr("src", res.target.result)
                }


            }

        })
	})
})(jQuery)